<?php

class CategorysController extends Controller {

    /**
     * @var string the default layout for the views. Defaults to '//layouts/column2', meaning
     * using two-column layout. See 'protected/views/layouts/column2.php'.
     */
    public $layout = 'default_page';


    /**
     *单面栏目
     * @param type $alias 
     */
    public function actionView($alias) {
        $model = Categorys::model()->findByAttributes(array('category_alias' => $alias));
        if ($model === null)
            throw new CHttpException(404, '页面不存在');
        
        //上级栏目
        $parentCategory = null;
        if($model->category_parent > 0)
        {
            $parentCategory = Categorys::model()->findByPk($model->category_parent);
        }
        
        $this->render('view', array(
            'model' => $model,
            'parentCategory' => $parentCategory,
        ));
    }

    /**
     *列表栏目
     * @param type $alias 
     */
    public function actionList($alias) {
        $model = Categorys::model()->findByAttributes(array('category_alias' => $alias));
        if ($model === null)
            throw new CHttpException(404, '页面不存在');
        
        $postModel = new Posts('search');
        $postModel->unsetAttributes();
        $postModel->category_id = $model->id;
        
        //上级栏目
        $parentCategory = null;
        if($model->category_parent > 0)
        {
            $parentCategory = Categorys::model()->findByPk($model->category_parent);
        }
        
        $this->render('list', array(
            'model' => $model,
            'postModel' => $postModel,
            'parentCategory' => $parentCategory,
        ));
    }
    
    /**
     * 问答栏目
     * @param type $alias 
     */
    public function actionFaq($alias){
        $model = Categorys::model()->findByAttributes(array('category_alias' => $alias));
        if ($model === null)
            throw new CHttpException(404, '页面不存在');
        
        $subCategorys = Categorys::model()->findAllByAttributes(array('category_parent' => $model->id));

        //上级栏目
        $parentCategory = null;
        if($model->category_parent > 0)
        {
            $parentCategory = Categorys::model()->findByPk($model->category_parent);
        }
        
        $this->render('faq', array(
            'model' => $model,
            'parentCategory' => $parentCategory,
            'subCategorys' => $subCategorys,
        ));
    }


    /**
     * Returns the data model based on the primary key given in the GET variable.
     * If the data model is not found, an HTTP exception will be raised.
     * @param integer the ID of the model to be loaded
     */
    public function loadModel($id) {
        $model = Categorys::model()->findByPk($id);
        if ($model === null)
            throw new CHttpException(404, 'The requested page does not exist.');
        return $model;
    }

    /**
     * Performs the AJAX validation.
     * @param CModel the model to be validated
     */
    protected function performAjaxValidation($model) {
        if (isset($_POST['ajax']) && $_POST['ajax'] === 'categorys-form') {
            echo CActiveForm::validate($model);
            Yii::app()->end();
        }
    }

}
